博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
apache commons-email1.3使用
阅读量:4586 次
发布时间:2019-06-09

本文共 5670 字,大约阅读时间需要 18 分钟。

apache commons-email1.3下载地址:

 

 

实例代码:

 

参考地址:

 

A simple text email

Our first example will create a basic email message to "John Doe" and send it through your Google Mail (GMail) account.

Email email = new SimpleEmail();email.setHostName("smtp.googlemail.com");email.setSmtpPort(465);email.setAuthenticator(new DefaultAuthenticator("username", "password"));email.setSSLOnConnect(true);email.setFrom("user@gmail.com");email.setSubject("TestMail");email.setMsg("This is a test mail ... :-)");email.addTo("foo@bar.com");email.send();

The call to setHostName("mail.myserver.com") sets the address of the outgoing SMTP server that will be used to send the message. If this is not set, the system property "mail.host" will be used.

Sending emails with attachments

To add attachments to an email, you will need to use the MultiPartEmail class. This class works just like SimpleEmail except that it adds several overloaded attach() methods to add attachments to the email. You can add an unlimited number of attachments either inline or attached. The attachments will be MIME encoded.

The simplest way to add the attachments is by using the EmailAttachment class to reference your attachments.

In the following example, we will create an attachment for a picture. We will then attach the picture to the email and send it.

import org.apache.commons.mail.*;...  // Create the attachment  EmailAttachment attachment = new EmailAttachment();  attachment.setPath("mypictures/john.jpg");  attachment.setDisposition(EmailAttachment.ATTACHMENT);  attachment.setDescription("Picture of John");  attachment.setName("John");  // Create the email message  MultiPartEmail email = new MultiPartEmail();  email.setHostName("mail.myserver.com");  email.addTo("jdoe@somewhere.org", "John Doe");  email.setFrom("me@apache.org", "Me");  email.setSubject("The picture");  email.setMsg("Here is the picture you wanted");  // add the attachment  email.attach(attachment);  // send the email  email.send();

You can also use EmailAttachment to reference any valid URL for files that you do not have locally. When the message is sent, the file will be downloaded and attached to the message automatically.

The next example shows how we could have sent the apache logo to John instead.

import org.apache.commons.mail.*;...  // Create the attachment  EmailAttachment attachment = new EmailAttachment();  attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));  attachment.setDisposition(EmailAttachment.ATTACHMENT);  attachment.setDescription("Apache logo");  attachment.setName("Apache logo");  // Create the email message  MultiPartEmail email = new MultiPartEmail();  email.setHostName("mail.myserver.com");  email.addTo("jdoe@somewhere.org", "John Doe");  email.setFrom("me@apache.org", "Me");  email.setSubject("The logo");  email.setMsg("Here is Apache's logo");    // add the attachment  email.attach(attachment);  // send the email  email.send();

Sending HTML formatted email

Sending HTML formatted email is accomplished by using the HtmlEmail class. This class works exactly like the MultiPartEmail class with additional methods to set the html content, alternative text content if the recipient does not support HTML email, and add inline images.

In this example, we will send an email message with formatted HTML content with an inline image.

import org.apache.commons.mail.HtmlEmail;...  // Create the email message  HtmlEmail email = new HtmlEmail();  email.setHostName("mail.myserver.com");  email.addTo("jdoe@somewhere.org", "John Doe");  email.setFrom("me@apache.org", "Me");  email.setSubject("Test email with inline image");    // embed the image and get the content id  URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");  String cid = email.embed(url, "Apache logo");    // set the html message  email.setHtmlMsg("The apache logo - ");  // set the alternative message  email.setTextMsg("Your email client does not support HTML messages");  // send the email  email.send();

First, notice that the call to embed() returns a String. This String is a randomly generated identifier that must be used to reference the image in the image tag.

Next, there was no call to setMsg() in this example. The method is still available in HtmlEmail but it should not be used if you will be using inline images. Instead, the setHtmlMsg() and setTextMsg() methods were used.

Sending HTML formatted email with embedded images

The previous example showed how to create a HTML email with embedded images but you need to know all images upfront which is inconvenient when using a HTML email template. The ImageHtmlEmail helps you solving this problem by converting all external images to inline images.

       import org.apache.commons.mail.HtmlEmail;...  // load your HTML email template  String htmlEmailTemplate = ....  // define you base URL to resolve relative resource locations  URL url = new URL("http://www.apache.org");  // create the email message  HtmlEmail email = new ImageHtmlEmail();  email.setDataSourceResolver(new DataSourceResolverImpl(url));  email.setHostName("mail.myserver.com");  email.addTo("jdoe@somewhere.org", "John Doe");  email.setFrom("me@apache.org", "Me");  email.setSubject("Test email with inline image");    // set the html message  email.setHtmlMsg(htmlEmailTemplate);  // set the alternative message  email.setTextMsg("Your email client does not support HTML messages");  // send the email  email.send(); 

First we create a HTML email template referencing some images. All referenced images are automatically transformed to inline images starting from the current working directory.

转载于:https://www.cnblogs.com/pengyan5945/p/5218340.html

你可能感兴趣的文章
读书笔记第一章
查看>>
Android 操作SQLite基本用法
查看>>
iis7 发布mvc3 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容...
查看>>
(vue.js)element ui 表单验证 this$refs[formName]validate里面的内容死活不执行
查看>>
启动多个appium服务(同时运行多台设备)
查看>>
Java大数相乘-hdu1063
查看>>
mysql-mmm 部署高可用集群
查看>>
solaris启动过程详解 分类: arm-linux-Ubuntu ...
查看>>
while循环和递归
查看>>
Linux下yum安装Redis
查看>>
.Net 下未捕获异常的处理
查看>>
[机器学习]-Adaboost提升算法从原理到实践
查看>>
AOP概念
查看>>
memset函数详细用法说明【转】
查看>>
php解析xml字符串
查看>>
SFTP客户端与服务端
查看>>
Modbus协议
查看>>
复位自动ID的问题有兩種方法
查看>>
CentOS 5.5 Samba服务器安装总结
查看>>
博客园评价
查看>>